<template>
  <el-dialog
    :title="!dataForm.liveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="直播主题" prop="liveTopic">
      <el-input v-model="dataForm.liveTopic" placeholder="直播主题"></el-input>
    </el-form-item>
    <el-form-item label="医院名称" prop="hospitalName">
      <el-input v-model="dataForm.hospitalName" placeholder="医院名称"></el-input>
    </el-form-item>
    <el-form-item label="医生等级" prop="doctorGrade">
      <el-input v-model="dataForm.doctorGrade" placeholder="医生等级"></el-input>
    </el-form-item>
    <el-form-item label="科室" prop="departmentName">
      <el-input v-model="dataForm.departmentName" placeholder="科室"></el-input>
    </el-form-item>
    <el-form-item label="直播创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="直播创建时间"></el-input>
    </el-form-item>
    <el-form-item label="直播状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="直播状态"></el-input>
    </el-form-item>
    <el-form-item label="热度" prop="hotDegree">
      <el-input v-model="dataForm.hotDegree" placeholder="热度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          liveId: 0,
          liveTopic: '',
          hospitalName: '',
          doctorGrade: '',
          departmentName: '',
          createTime: '',
          status: '',
          hotDegree: ''
        },
        dataRule: {
          liveTopic: [
            { required: true, message: '直播主题不能为空', trigger: 'blur' }
          ],
          hospitalName: [
            { required: true, message: '医院名称不能为空', trigger: 'blur' }
          ],
          doctorGrade: [
            { required: true, message: '医生等级不能为空', trigger: 'blur' }
          ],
          departmentName: [
            { required: true, message: '科室不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '直播创建时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '直播状态不能为空', trigger: 'blur' }
          ],
          hotDegree: [
            { required: true, message: '热度不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.liveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.liveId) {
            this.$http({
              url: this.$http.adornUrl(`/doctor/doctorlive/info/${this.dataForm.liveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.liveTopic = data.doctorLive.liveTopic
                this.dataForm.hospitalName = data.doctorLive.hospitalName
                this.dataForm.doctorGrade = data.doctorLive.doctorGrade
                this.dataForm.departmentName = data.doctorLive.departmentName
                this.dataForm.createTime = data.doctorLive.createTime
                this.dataForm.status = data.doctorLive.status
                this.dataForm.hotDegree = data.doctorLive.hotDegree
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/doctor/doctorlive/${!this.dataForm.liveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'liveId': this.dataForm.liveId || undefined,
                'liveTopic': this.dataForm.liveTopic,
                'hospitalName': this.dataForm.hospitalName,
                'doctorGrade': this.dataForm.doctorGrade,
                'departmentName': this.dataForm.departmentName,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status,
                'hotDegree': this.dataForm.hotDegree
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
